package com.zq.widget.inverted.game.resource;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;

import com.dd.plist.NSDictionary;
import com.dd.plist.NSObject;
import com.dd.plist.PropertyListFormatException;
import com.dd.plist.PropertyListParser;

import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by zhangqiang on 2018/3/30.
 */
public class ResourceLoader {


    public static List<InfoBean> loadInfo(Context context) {


        try {
            InputStream inputStream = context.getAssets().open("airfightSheet.plist");
            NSDictionary nsDictionary = (NSDictionary) PropertyListParser.parse(inputStream);
            NSDictionary framesDictionary = (NSDictionary) nsDictionary.objectForKey("frames");
            List<InfoBean> resourceBeans = new ArrayList<>();
            for (Map.Entry<String, NSObject> entry : framesDictionary.entrySet()) {

                String key = entry.getKey();
                NSDictionary value = ((NSDictionary) entry.getValue());
                String spriteColorRect = value.objectForKey("spriteColorRect").toJavaObject().toString();
                String spriteOffset = value.objectForKey("spriteOffset").toJavaObject().toString();
                String spriteSize = value.objectForKey("spriteSize").toJavaObject().toString();
                String spriteSourceSize = value.objectForKey("spriteSourceSize").toJavaObject().toString();
                String spriteTrimmed = value.objectForKey("spriteTrimmed").toJavaObject().toString();
                String textureRect = value.objectForKey("textureRect").toJavaObject().toString();
                String textureRotated = value.objectForKey("textureRotated").toJavaObject().toString();

                InfoBean resourceBean = new InfoBean();
                resourceBean.setName(key);

                String pattern = "[{}]";

                spriteColorRect = spriteColorRect.replaceAll(pattern, "");
                String[] strings = spriteColorRect.split(",");
                resourceBean.setSpriteColorRect(new Rect(Integer.valueOf(strings[0].trim()),
                        Integer.valueOf(strings[1].trim()),
                        Integer.valueOf(strings[2].trim()),
                        Integer.valueOf(strings[3].trim())));

                spriteOffset = spriteOffset.replaceAll(pattern, "");
                strings = spriteOffset.split(",");
                resourceBean.setSpriteOffset(new Point(Integer.valueOf(strings[0]), Integer.valueOf(strings[0].trim())));

                spriteSize = spriteSize.replaceAll(pattern, "");
                strings = spriteSize.split(",");
                resourceBean.setSpriteSize(new InfoBean.Size(Integer.valueOf(strings[0].trim()), Integer.valueOf(strings[0].trim())));

                spriteSourceSize = spriteSourceSize.replaceAll(pattern, "");
                strings = spriteSourceSize.split(",");
                resourceBean.setSpriteSourceSize(new InfoBean.Size(Integer.valueOf(strings[0].trim()), Integer.valueOf(strings[0].trim())));

                textureRect = textureRect.replaceAll(pattern, "");
                strings = textureRect.split(",");
                resourceBean.setTextureRect(new Rect(Integer.valueOf(strings[0].trim()),
                        Integer.valueOf(strings[1].trim()),
                        Integer.valueOf(strings[2].trim()),
                        Integer.valueOf(strings[3].trim())));

                resourceBean.setTextureRotated(Boolean.valueOf(textureRotated));

                spriteColorRect = spriteColorRect.replaceAll(pattern, "");
                strings = spriteColorRect.split(",");
                resourceBean.setSpriteColorRect(new Rect(Integer.valueOf(strings[0].trim()),
                        Integer.valueOf(strings[1].trim()),
                        Integer.valueOf(strings[2].trim()),
                        Integer.valueOf(strings[3].trim())));

                resourceBean.setSpriteTrimmed(Boolean.valueOf(spriteTrimmed));

                resourceBeans.add(resourceBean);
            }
            return resourceBeans;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (PropertyListFormatException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String,ResourceBean> loadResource(Context context) {

        try {
            Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open("airfightSheet.png"));

            List<InfoBean> infoBeans = loadInfo(context);

            if (infoBeans == null) {
                return null;
            }

            HashMap<String,ResourceBean> resourceBeans = new HashMap<>();

            for (InfoBean infoBean : infoBeans) {

                Rect textureRect = infoBean.getTextureRect();

                Bitmap sourceBitmap = Bitmap.createBitmap(bitmap, textureRect.left, textureRect.top, textureRect.right, textureRect.bottom, null, true);

                ResourceBean resourceBean = new ResourceBean();
                resourceBean.setInfoBean(infoBean);
                resourceBean.setBitmap(sourceBitmap);
                resourceBeans.put(infoBean.getName(),resourceBean);
            }
            return resourceBeans;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
